package com.spvs.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spvs.approval.domain.ApprovalSubmitEntity;
import com.spvs.approval.mapper.ApprovalSubmitMapper;
import com.spvs.approval.service.ApprovalSubmitService;
import com.spvs.approval.utils.DateUtils;
import com.spvs.approval.utils.HttpContextUtils;
import com.spvs.approval.utils.MapUtils;
import com.spvs.approval.utils.ToolUtil;
import com.spvs.common.core.web.domain.AjaxResult;
import com.spvs.common.security.utils.SecurityUtils;
import com.spvs.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Service("basicApprovalSubmitService")
@Transactional
public class ApprovalSubmitServiceImpl extends ServiceImpl<ApprovalSubmitMapper, ApprovalSubmitEntity> implements ApprovalSubmitService {

    @Override
    public Page<ApprovalSubmitEntity> queryPage(Map<String, Object> params) {
        QueryWrapper<ApprovalSubmitEntity> wrapper = new QueryWrapper<>();
        if (params.containsKey("condition")) {
            wrapper.allEq(MapUtils.StringToMap((String)params.get("condition")));
        }

        if (params.get("keyWord")!=null && StringUtils.isNotBlank((String)params.get("keyWord"))){
            wrapper.and(wq -> {
                wq.like("title",(String)params.get("keyWord")).or().like("code",(String)params.get("keyWord"));
            });
        }
        if (params.get("startTime")!=null && StringUtils.isNotBlank((String)params.get("startTime"))&&params.get("endTime")!=null && StringUtils.isNotBlank((String)params.get("endTime"))){
            wrapper.between("create_time", DateUtils.stringToDate((String)params.get("startTime"),DateUtils.DATE_TIME_FORMAT), DateUtils.stringToDate((String)params.get("endTime"),DateUtils.DATE_TIME_FORMAT));
        }
        wrapper.orderByDesc("create_time");
        Page<ApprovalSubmitEntity> page = new Page<>(Long.parseLong((String)params.get("pageIndex")), Long.parseLong((String)params.get("pageSize")));
        return this.page(page, wrapper);
    }

    @Override
    public Page<ApprovalSubmitEntity> queryAppPage(Map<String, Object> params) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        if(ToolUtil.isEmpty(token)){
            return new Page<>();
        }
        QueryWrapper<ApprovalSubmitEntity> wrapper = new QueryWrapper<>();
        if (params.containsKey("condition")) {
            wrapper.allEq(MapUtils.StringToMap((String)params.get("condition")));
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        wrapper.eq("approval_staff_id", loginUser.getUserid()).orderByDesc("create_time");
        Page<ApprovalSubmitEntity> page = new Page<>(Long.parseLong((String)params.get("pageIndex")), Long.parseLong((String)params.get("pageSize")));
        return this.page(page, wrapper);
    }

    @Override
    public Page<ApprovalSubmitEntity> approvePage(Map<String, Object> params) {
        QueryWrapper<ApprovalSubmitEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("approval_staff_id",params.get("userId"));
        wrapper.orderByDesc("create_time");
        Page<ApprovalSubmitEntity> page = new Page<>(Long.parseLong((String)params.get("pageIndex")), Long.parseLong((String)params.get("pageSize")));
        return this.page(page, wrapper);
    }

    @Override
    public Page<ApprovalSubmitEntity> userApprovedList(Map<String, Object> params) {
        QueryWrapper<ApprovalSubmitEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("approval_staff_id",params.get("userId")).orderByDesc("create_time");

        Page<ApprovalSubmitEntity> page = new Page<>(Long.parseLong((String)params.get("pageIndex")), Long.parseLong((String)params.get("pageSize")));
        return this.page(page, wrapper);
    }

    @Override
    public Page<ApprovalSubmitEntity> userSubmitList(Map<String, Object> params) {
        QueryWrapper<ApprovalSubmitEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("create_user_id",params.get("userId")).orderByDesc("create_time");

        Page<ApprovalSubmitEntity> page = new Page<>(Long.parseLong((String)params.get("pageIndex")), Long.parseLong((String)params.get("pageSize")));
        return this.page(page, wrapper);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public AjaxResult approvalSubmit(SysUserEntity sysUserEntity, Integer id, Integer status, String remark, String ipStr){
//        return AjaxResult.success();
//    }

    @Override
    public int selectApprovalByCreateUserId(int approvalStatus, int createUserId) {
        return baseMapper.selectApprovalByCreateUserId(approvalStatus, createUserId);
    }

    @Override
    public int selectApprovalByStaffId(int approvalStatus, String staffId) {
        return baseMapper.selectApprovalByStaffId(approvalStatus, staffId);    }

    @Override
    public AjaxResult selectApprovalSubmitVo(String businessId) {
        return AjaxResult.success(baseMapper.selectApprovalSubmitVo(businessId));
    }

}